package com.example.springsecuritydemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springsecuritydemo.mapper.UsersMapper;
import com.example.springsecuritydemo.model.Users;
import com.example.springsecuritydemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author Zenan Chen
 * @date 2023/4/13 16:26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UsersMapper, Users> implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public void registerUser(Users user) {
        LambdaQueryWrapper<Users> queryWrapper = Wrappers.lambdaQuery(Users.class).eq(Users::getUserName, user.getUserName());
        Users users = usersMapper.selectOne(queryWrapper);
        if (users != null) {
            throw new RuntimeException("用户名已存在！");
        }
        user.setUserPwd(passwordEncoder.encode(user.getUserPwd()));
        usersMapper.insert(user);
    }
}
